# encoding=utf-8
from LBT.BaseParent.BaseLbt import lbtlogin
from LBT.Resource.SmsAtt import SmsAtt


class smsCount(lbtlogin, SmsAtt):
    """短信统计模块
    1、页面校验，包含title和列表头部
    2、如果班主任列表有数据，先尝试找有短信数量的，如果找不到就找第一个
    然后去数据库校验短信数量是否正确
    """

    @lbtlogin.loginFun
    def test_checkSmsCount(self):
        """
        检查短信统计模块页面加载是否正确。
        :return:
        """
        self.docstr='检查短信统计模块页面加载是否正确'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.smsNavigation_class, self.sms_module_smscount_url)
        self.assertTrue(dr.find_element_by_xpath(self.sms_smscount_title_xpath).is_displayed())
        # 循环校验页面top信息
        for tt in self.sms_smscount_top_list:
            istrue = dr.find_element_by_xpath(self.sms_smscount_top_xpath + str(tt) + '"]').is_displayed()
            self.assertTrue(istrue)

    @lbtlogin.loginFun
    def test_smsNumberCheck(self):
        """
        如果班主任列表有数据，先尝试找有短信数量的，如果找不到就找第一个
        然后去数据库校验短信数量是否正确
        :return:
        """
        self.docstr = '如果班主任列表有数据，先尝试找有短信数量的，如果找不到就找第一个然后去数据库校验短信数量是否正确'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.smsNavigation_class, self.sms_module_smscount_url)
        infolist = dr.find_elements_by_xpath(self.sms_smscount_list_info_xpath)
        if infolist.__len__() == 0:
            return True
        else:
            infodict = {}
            count = 0
            for info in infolist:
                count = info.find_elements_by_tag_name("td")[4].text
                if int(count) > 0:
                    infodict["name"] = info.find_elements_by_tag_name("td")[1].text
                    infodict["phone"] = info.find_elements_by_tag_name("td")[2].text
                    infodict["eduunit"] = info.find_elements_by_tag_name("td")[3].text
                    break
            if infodict.__len__() == 0:
                infodict["name"] = infolist[0].find_elements_by_tag_name("td")[1].text
                infodict["phone"] = infolist[0].find_elements_by_tag_name("td")[2].text
                infodict["eduunit"] = infolist[0].find_elements_by_tag_name("td")[3].text
            sql = "SELECT COUNT (t.SmsSendid) FROM ( SELECT t1.SmsSendID, t1.UsersID, SendTime, SmsContent, EduUnitID, DeptID  FROM SmsSend t1 INNER JOIN SmsReceive t2 ON t1.SmsSendID = t2.SmsSendID WHERE t1.usersid =(  select usersid from users where phone = '" + \
                  infodict["phone"] + "' and state=1 and name = '" + infodict[
                      "name"] + "' and usertype =1 and schoolid in (select schoolid from school where schoolname like '" + self.lbt_schoolname_text + "')) AND t1.OutBoxState = 1 AND t2.EduUnitID = ( select eduunitid from eduunit  where level = 5 and state =1 and eduunitname = '" + \
                  infodict[
                      "eduunit"] + "' and schoolid in (select schoolid from school where schoolname like '" + self.lbt_schoolname_text + "')) AND SendTime BETWEEN  (select cast(dateadd(day,-day(getdate())+1,getdate()) as date))  AND (  SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))) GROUP BY t1.SmsSendID, t1.UsersID, SendTime, SmsContent, EduUnitID, DeptID ) t"
            querycount = self.selectlbt(sql)
            self.assertTrue(count == querycount)
